package grape.workflow.rest.flowable.ui.modeler;

import org.flowable.ui.modeler.conf.Bootstrapper;
import org.flowable.ui.modeler.properties.FlowableModelerAppProperties;
import org.flowable.ui.modeler.service.FlowableDecisionTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * 重写 modeler ui的bootstrap {@link Bootstrapper}
 * Created by yangwei
 * Created at 2020/6/8 17:42
 */
@Component
public class ModelerBootstrapper implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private FlowableDecisionTableService decisionTableService;

    @Autowired
    private FlowableModelerAppProperties modelerAppProperties;
    private Boolean inited = false;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null || event.getApplicationContext().getParent().getParent() == null) { // Using Spring MVC, there are multiple child contexts. We only care about the root
            if (inited) {
                return;
            }
            if (modelerAppProperties == null || modelerAppProperties.isDecisionTableMigrationEnabled()) {
                decisionTableService.migrateDecisionTables();
            }
            inited = true;
        }
    }
}
